 
package ibms_rcp.pay.parts;

import ibms_rcp.dao.CompanyManager;
import ibms_rcp.dao.EstateManager;
import ibms_rcp.model.Building;
import ibms_rcp.model.Company;
import ibms_rcp.model.Floor;
import ibms_rcp.model.Room;
import ibms_rcp.verify.CompanyVerify;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.nebula.widgets.calendarcombo.CalendarCombo;

/**
 * 交物业费界面
 * @author diamond-wang
 *
 */
public class PropertyPart {
	// 楼栋
	private ComboViewer buildingComboViewer;
	private Combo buildingCombo;
	// 楼层
	private ComboViewer floorComboViewer;
	private Combo floorCombo;
	// 房间
	private ComboViewer roomComboViewer;
	private Combo roomCombo;
	
	//交费起始时间
	private CalendarCombo dateFromCombo;
	//交费截止时间
	private CalendarCombo dateToCombo;
	//交费时长 取3个月或6个月
	private int duration;
	private Button threeButton;
	private Button sixButton;
	//计月方式 ture为自然月 false为按日期推算
	private boolean monthMode;
	private Button natureButton;
	private Button calcButton;
	//提醒日期
	private CalendarCombo remindCombo;
	//交费金额
	private Text totalText;
	//交费按钮
	private Button payButton;
	
	@Inject
	public PropertyPart(Composite parent) {
	}
	
	@PostConstruct
	public void postConstruct(final Composite parent) {
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		parent.setLayout(layout);
		
		Label title = new Label(parent, SWT.NONE);
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setText("交物业费");
		
		Composite payComposite = new Composite(parent, SWT.BORDER);
		payComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		payComposite.setLayout(new GridLayout(6, false));
		
		// 楼栋
		Label buildingLabel = new Label(payComposite, SWT.RIGHT);
		buildingLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		buildingLabel.setText("楼栋：");
		buildingComboViewer = new ComboViewer(payComposite, SWT.READ_ONLY);
		buildingCombo = buildingComboViewer.getCombo();
		buildingCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		buildingComboViewer.setContentProvider(ArrayContentProvider
				.getInstance());
		buildingComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Building) {
					Building building = (Building) element;
					return building.getEms_name();
				}
				return super.getText(element);
			}
		});
		// 获取楼数据
		List<Building> buildings = EstateManager.getBuildings();
		buildingComboViewer.setInput(buildings);

		buildingComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
						long buildingId = ((Building) selection
								.getFirstElement()).getId();
						// 获取楼层数据
						List<Floor> floors = EstateManager
								.getFloorsByBuilding(buildingId);
						floorComboViewer.setInput(floors);
					}
				});

		// 楼层
		Label floorLabel = new Label(payComposite, SWT.RIGHT);
		floorLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		floorLabel.setText("楼层：");
		floorComboViewer = new ComboViewer(payComposite, SWT.READ_ONLY);
		floorCombo = floorComboViewer.getCombo();
		floorCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		floorComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		floorComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Floor) {
					Floor floor = (Floor) element;
					return floor.getEms_name();
				}
				return super.getText(element);
			}
		});

		floorComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
						long floorId = ((Floor) selection.getFirstElement())
								.getId();
						// 获取楼层数据
						List<Room> rooms = EstateManager.getRoomsByFloor(floorId);
						roomComboViewer.setInput(rooms);
					}
				});

		// 房间号
		Label roomLabel = new Label(payComposite, SWT.RIGHT);
		roomLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		roomLabel.setText("房间：");
		roomComboViewer = new ComboViewer(payComposite, SWT.READ_ONLY);
		roomCombo = roomComboViewer.getCombo();
		roomCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		roomComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		roomComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Room) {
					Room room = (Room) element;
					return room.getEms_name();
				}
				return super.getText(element);
			}
		});
		roomComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
					}
				});
		
		// 交费时长
		Label durationLabel = new Label(payComposite, SWT.RIGHT);
		durationLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		durationLabel.setText("交费时长：");
		threeButton = new Button(payComposite, SWT.RADIO);
		threeButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		threeButton.setText("3个月");
		threeButton.setSelection(true);
		sixButton = new Button(payComposite, SWT.RADIO);
		sixButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		sixButton.setText("6个月");
		new Label(payComposite, SWT.NONE);
		new Label(payComposite, SWT.NONE);
		new Label(payComposite, SWT.NONE);
		
		// 计月方式
		Label monthModeLabel = new Label(payComposite, SWT.RIGHT);
		monthModeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		monthModeLabel.setText("计月方式：");
		natureButton = new Button(payComposite, SWT.RADIO);
		natureButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		natureButton.setText("自然月");
		natureButton.setSelection(true);
		calcButton = new Button(payComposite, SWT.RADIO);
		calcButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		calcButton.setText("推算");
		new Label(payComposite, SWT.NONE);
		new Label(payComposite, SWT.NONE);
		new Label(payComposite, SWT.NONE);
		
		// 起始日期
		Label dateFromLabel = new Label(payComposite, SWT.RIGHT);
		dateFromLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		dateFromLabel.setText("起始日期：");
		dateFromCombo = new CalendarCombo(payComposite, SWT.BORDER);
		dateFromCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		
		// 截止日期
		Label dateToLabel = new Label(payComposite, SWT.RIGHT);
		dateToLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		dateToLabel.setText("截止时间：");
		dateToCombo = new CalendarCombo(payComposite, SWT.BORDER);
		dateToCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		dateToCombo.setEnabled(false);
		
		// 提醒日期
		Label remindLabel = new Label(payComposite, SWT.RIGHT);
		remindLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		remindLabel.setText("提醒时间：");
		remindCombo = new CalendarCombo(payComposite, SWT.BORDER);
		remindCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		
		// 交费金额
		Label totalLabel1 = new Label(payComposite, SWT.RIGHT);
		totalLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		totalLabel1.setText("交费金额：");
		totalText = new Text(payComposite, SWT.BORDER);
		totalText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		totalText.setEditable(false);
		Label totalLabel2 = new Label(payComposite, SWT.RIGHT);
		totalLabel2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1));
		totalLabel2.setText("元");
				
		//交费按钮
		payButton = new Button(payComposite, SWT.NONE);
		payButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 7, 1));
		payButton.setText("确认交费");
		payButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				// 点击时
				//选中threeButton时交费时长设置为3个月，否则是6个月
				duration = threeButton.getSelection()? 3:6;
				//选中natureButton时 计月方式设置为自然月，否则为推算
				monthMode = natureButton.getSelection(); 
				//TODO 
				//添加业主的充值记录
				//添加房间的物业费消费记录
				System.out.println("payButton mouse up.");
			}
		});
		
	}
	
	
	@PreDestroy
	public void preDestroy() {
		//TODO Your code here
	}
}